<template>
	<view class="orders" :class="orderList.length==0?'empty':''">
		<block v-if="orderList.length>0">
			<u-list @scrolltolower="scrolltolower">
				<u-list-item v-for="(item, index) in orderList" :key="index" :class="item.orderState==3?'qx':''"
					@tap.prevent="toDetails(item)">
					<view class="top">
						<view class="left">
							<image src="@/static/my/cp_zs.png" mode="" v-if="item.orderState!=3"></image>
							<image src="@/static/my/cp_zs_hs.png" mode="" v-else></image>
							<text>船票</text>
						</view>
						<view class="right">
							<block v-if="item.orderState==0&&item.djsTime">
								<uCountDown @finish="finish(item)" :djsTime="item.djsTime" />
							</block>
							<block v-else>
								<text
									:class="item.orderState==2?'textColor111':(oscConfig[item.orderState]||'textColor999')">{{ocfg[item.orderState]}}</text>
							</block>
						</view>
					</view>
					<view class="content">
						<view class="title">
							<text>{{item.startPortName}} - {{item.endPortName}} - {{item.lineNum+''+item.sx}}</text>
							<text>￥{{item.orderRealityPay}}</text>
						</view>
						<view class="date">
							<text v-if="item.qpNum>0">全票 X {{item.qpNum}},</text>
							<text v-if="item.etpNum>0">儿童票 X {{item.etpNum}},</text>
							<text>共{{item.qpNum+item.etpNum}}张</text>
						</view>
						<view class="room_price">
							<text>开船时间：{{item.kcTime}}</text>
						</view>
					</view>
					<view class="btns" v-if="['1','2','5'].includes(item.orderState)">
						<!-- <view @tap.stop="applyAfterSales(item)" class="btn_blue">申请售后</view> -->
						<view @tap.stop="refundTicket(item)" class="btn_blue">退票</view>
						<view @tap.stop="changeTicket(item)" class="btn_blue">改签</view>
					</view>
					<view class="btns" v-if="item.orderState==7&&!item.noRefund">
						<view @tap.stop="refundTicket(item)" class="btn_blue">退票</view>
					</view>
					<view class="btns" v-if="item.orderState==0">
						<view @tap.stop="cancelOrder(item)" class="btn_blue">取消</view>
						<view @tap.stop="toPay(item)" class="btn_blue " style="background-color:#FF4141 ; color: #fff;">
							去支付</view>
					</view>
					<view class="btns" v-if="item.orderState==3">
						<view @tap.stop="deleteOrder(item)" class="btn_blue">删除</view>
					</view>

					<view class="btns" v-if="item.orderState==20">
						<!-- <view @tap.stop="toEvaluate(item)" class="btn_blue">评价</view> -->
					</view>
				</u-list-item>
			</u-list>
		</block>
		<block v-else>
			<NoData style="margin: 0 100%;" />
		</block>

		<!-- 删除弹框 -->
		<u-modal :show="showdelete" :title="title" :content='contented' showCancelButton="true" confirmText='确定'
			confirmColor='#007A69' @confirm="confirmed" @cancel='cancel'></u-modal>
	</view>
</template>

<script>
	export default {
		options: {
			styleIsolation: 'shared'
		},
		data() {
			return {
				mysearch: '',
				orderList: [],
				oscConfig: {
					1: 'textColor333',
					0: 'textColor000',
					5: 'textColor000'
				},
				pageIndex: 1,
				pageCount: 10,
				isOver: false,
				ocfg: this.$ocfg,
				orderState: '',
				showdelete: false,
				title: '温馨提示',
				contented: '您确定要删除吗？确定删除将不可撤回！',
				delId: null
			}
		},
		methods: {
			// 取消
			cancel() {
				this.showdelete = false
			},
			// 删除
			deleteOrder(item) {
				this.showdelete = true
				this.delId = item.id
			},
			// 确定删除
			confirmed() {
				let ids = []
				ids.push(this.delId)
				this.$api.del('/api/travel/api/ship/order/deleteOrder/',
					ids
				).then((res) => {
					if (res.data.code == 0) {
						this.$showToast('删除成功');
						this.showdelete = false
						setTimeout(() => {
							this.init();
						}, 300)
					} else {
						this.$showModal(res.data.msg);
						this.showdelete = false
						setTimeout(() => {
							this.init();
						}, 300)
					}
				})
			},
			scrolltolower() {
				if (this.isOver) return;
				this.getList();
			},
			init() {
				this.pageIndex = 1;
				this.isOver = false;
				this.orderList = [];
				this.getList();
			},
			search(keyword) {
				this.mysearch = keyword;
				this.pageIndex = 1;
				this.isOver = false;
				this.orderList = [];
				this.getList();
			},
			getList() {
				let that = this;
				this.$api.get('/api/travel/isshiporder/page', {
					openId: JSON.parse(uni.getStorageSync('userInfo')).openId,
					orderState: this.orderState,
					page: this.pageIndex,
					limit: this.pageCount,
					search: this.mysearch,
					orderType: '3,9,22'
				}).then(res => {
					if (res.data.code === 0) {
						that.pageIndex++;
						let {
							list,
							total
						} = res.data.data;
						if (that.orderList.length + list.length > total || list.length == 0) return that.isOver =
							true;
						that.orderList = [...that.orderList, ...list];
						that.orderList.forEach((d, i) => {
							d.qpNum = d.detailDTOList.filter(f => f.priceType == 1).length;
							d.etpNum = d.detailDTOList.filter(f => f.priceType == 2).length;
							d.kcTime = new Date(d.sailDate).Format('yyyy年MM月dd日') + ' ' + d.sailTime;
							d.noRefund = d.detailDTOList.length == d.detailDTOList.reduce((cur, pre) =>
								cur + (['6', '7'].includes(pre.itemState) ? 1 : 0), 0);
							if (d.orderState == 0) {
								let diffMS = Date.parse(new Date(d.expireTime)) - Date.parse(new Date());
								if (diffMS > 1000) d.djsTime = diffMS - 1000; //提前一秒，便于取消刷新
							}
						})
					} else that.$showToast(res.data.msg);
				})
			},
			changeStatus(index) {
				if (index === 0) this.orderState = '';
				else if (index === 1) this.orderState = '0';
				else if (index === 2) this.orderState = '1,2,5';
				else if (index === 3) this.orderState = '20';
				else this.orderState = '-2,-1,3,4,6,7,8,9,10,11';
				this.init();
			},
			//申请售后
			applyAfterSales(item) {
				this.$showToast('正在开发中...');
			},
			//退票
			refundTicket(item) {
				uni.navigateTo({
					url: '/pagesMy/shipTicketOrder/refundTicket?item=' + JSON.stringify(item)
				})
			},
			//改签
			changeTicket(item) {
				uni.navigateTo({
					url: '/pagesMy/shipTicketOrder/changeTicket?item=' + JSON.stringify(item)
				})
			},
			//取消订单
			cancelOrder(item) {
				this.$api.post('/travel/api/ship/order/cancel', {
					orderId: item.orderId
				}).then(res => {
					if (res.data.code === 0) {
						this.$showToast('订单已取消');
						this.init();
					} else this.$showModal(res.data.msg)
				})
			},
			//去支付
			toPay(item) {
				this.$api.get('/travel/isshiporder/getOrderAndDetail/' + item.orderId).then(resu => {
					if (resu.data.code === 0) {
						if(resu.data.data.orderState==0){
							this.$api.post('/order/icbc/createOrder', {
								orderCode: item.orderId,
								tradeType: 'JSAPI',
								openId: JSON.parse(uni.getStorageSync('userInfo')).openId
							}, false).then(res => {
								if (res.statusCode == 200 && res.data != null) {
									this.$wxPay(res.data).then(result => {
										this.$showToast('支付成功');
										this.init();
									})
								}
							})
						}else this.$showToast('不可支付，请稍后刷新列表');
					}else this.$showToast(resu.data.msg);
				})
			},
			toDetails(item) {
				uni.navigateTo({
					url: '/pagesMy/shipTicketOrder/details?orderId=' + item.orderId
				})
			},
			returnBack() {
				uni.reLaunch({
					url: '/pages/my/index'
				})
			},
			toEvaluate(item) {
				item.guideId = item.id;
				item.sourceType = 3;
				item.orderCode = item.orderId;
				uni.navigateTo({
					url: '/pagesMy/appraise/appraise?obj=' + JSON.stringify(item)
				})
			},
			finish(item) {
				this.$api.post('/travel/api/ship/order/cancel', {
					orderId: item.orderId
				}).then(res => {
					setTimeout(() => {
						this.init();
					}, 500)
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.orders {
		display: flex;
		align-items: center;

		&.empty {
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		::v-deep .u-list-item {
			width: 100%;
			margin-top: 20rpx;
			border-radius: 16rpx;
			background: #fff;
			padding: 36rpx 24rpx 24rpx;
			box-sizing: border-box;

			.top {
				display: flex;
				justify-content: space-between;

				.left {
					display: flex;
					align-items: center;

					image {
						width: 36rpx;
						height: 36rpx;
					}

					text {
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
						line-height: 28rpx;
						margin-left: 20rpx;
					}
				}

				.right {
					text-align: right;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					line-height: 37rpx;
				}
			}

			.content {
				padding: 22rpx 0 24rpx 56rpx;
				background: #fff;

				.title {
					display: flex;
					align-items: center;
					justify-content: space-between;

					text {
						font-size: 32rpx;
						font-family: PingFang-SC-Bold, PingFang-SC;
						font-weight: bold;
						color: #333333;
						line-height: 45rpx;

						&:last-child {
							color: #111111;
						}
					}
				}

				.date {
					display: flex;
					align-items: center;
					margin-top: 22rpx;

					&>text {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #AAAAAA;
						line-height: 24rpx;
					}
				}

				.room_price {
					margin-top: 16rpx;
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #AAAAAA;
				}
			}

			.btns {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				border-top: 1rpx solid #EFEFEF;

				&>view {
					margin: 24rpx 0 0 24rpx;
					width: 148rpx;
					height: 56rpx;
					border: 1rpx solid #EFEFEF;
					border-radius: 16rpx;
					line-height: 56rpx;
					text-align: center;
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;

					&.btn_blue {
						// border: 1rpx solid #007A69;
						color: #333333;
						background-color: #F6F6F6;
					}
				}
			}

			&.qx {
				text {
					color: #999999 !important;
				}
			}

			&:last-child {
				margin-bottom: 40rpx;
			}
		}
	}

	.textColor999 {
		color: #999999;
	}

	.textColor111 {
		color: #111111 !important;
	}

	.textColor333 {
		color: #333333;
	}

	.textColor000 {
		color: #FEB000;
	}
</style>